package main

import (
	"fmt"
	"strings"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/dialog"
	"fyne.io/fyne/v2/widget"
)

func main() {
	a := app.New()
	w := a.NewWindow("FmtGo v1.0.4-beta-20250401")
	w.Resize(fyne.NewSize(600, 420))

	input := widget.NewMultiLineEntry()
	input.SetPlaceHolder("请输入字符串")
	input.Resize(fyne.NewSize(200, 100)) // 设置输入框的固定宽度和高度

	output := widget.NewMultiLineEntry()
	output.Resize(fyne.NewSize(100, 100)) // 使用多行文本框以支持换行显示
	output.Disable()                      // 禁用输出框的编辑功能

	convertBtn := widget.NewButton("行转逗号格式", func() {
		inputStr := input.Text
		lines := strings.Split(inputStr, "\n") // 按行分割输入文本
		var filteredLines []string

		for _, line := range lines {
			trimmed := strings.TrimSpace(line)
			if trimmed != "" {
				filteredLines = append(filteredLines, trimmed)
			}
		}

		if len(filteredLines) == 0 {
			output.SetText("没有有效的输入")
			return
		}

		result := strings.Join(filteredLines, ",") // 使用换行符连接结果
		output.SetText(result)
	})

	convertSQLBtn := widget.NewButton("行转SQL格式", func() {
		inputStr := input.Text
		lines := strings.Split(inputStr, "\n") // 按行分割输入文本
		var filteredLines []string

		for _, line := range lines {
			trimmed := strings.TrimSpace(line)
			if trimmed != "" {
				filteredLines = append(filteredLines, trimmed)
			}
		}

		if len(filteredLines) == 0 {
			output.SetText("没有有效的输入")
			return
		}

		result := strings.Join(filteredLines, "\",\"") // 使用换行符连接结果
		result = "\"" + result + "\""
		output.SetText(result)
	})

	CommaConvertSQLBtn := widget.NewButton("逗号转SQL格式", func() {
		inputStr := input.Text
		lines := strings.Split(inputStr, ",") // 按行分割输入文本
		var filteredLines []string

		for _, line := range lines {
			trimmed := strings.TrimSpace(line)
			if trimmed != "" {
				filteredLines = append(filteredLines, trimmed)
			}
		}

		if len(filteredLines) == 0 {
			output.SetText("没有有效的输入")
			return
		}

		result := strings.Join(filteredLines, "\",\"") // 使用换行符连接结果
		result = "\"" + result + "\""
		output.SetText(result)
	})

	// 创建逗号转行按钮
	commaToLineBtn := widget.NewButton("逗号转行", func() {
		inputStr := input.Text
		lines := strings.Split(inputStr, ",") // 按逗号分割输入文本
		var filteredLines []string

		for _, line := range lines {
			trimmed := strings.TrimSpace(line)
			if trimmed != "" {
				filteredLines = append(filteredLines, trimmed)
			}
		}

		if len(filteredLines) == 0 {
			output.SetText("没有有效的输入")
			return
		}

		result := strings.Join(filteredLines, "\n") // 使用换行符连接结果
		output.SetText(result)
	})

	appendCredBtn := widget.NewButton("ansible模版生成", func() {
		inputStr := input.Text
		lines := strings.Split(inputStr, "\n") // 按行分割输入文本
		var filteredLines []string

		for _, line := range lines {
			trimmed := strings.TrimSpace(line)
			if trimmed != "" {
				filteredLines = append(filteredLines, trimmed)
			}
		}

		if len(filteredLines) == 0 {
			output.SetText("没有有效的输入")
			return
		}

		// 创建一个表单对话框来获取账号和密码
		entryAccount := widget.NewEntry()
		entryAccount.SetPlaceHolder("账号")

		entryPassword := widget.NewPasswordEntry()
		entryPassword.SetPlaceHolder("密码")

		// 创建自定义对话框
		form := dialog.NewCustomConfirm("请输入账号密码", "生成模版", "关闭窗口", container.NewVBox(
			entryAccount,
			entryPassword,
		), func(confirmed bool) {
			if confirmed {
				// 用户点击了“确定”按钮
				account := entryAccount.Text
				password := entryPassword.Text

				if account == "" || password == "" {
					dialog.ShowError(fmt.Errorf("账号或密码不能为空"), w)
					return
				}

				var resultLines []string
				resultLines = append(resultLines, "[all:vars]")
				resultLines = append(resultLines, "ansible_ssh_user='"+account+"'")
				resultLines = append(resultLines, "ansible_ssh_pass='"+password+"'")
				resultLines = append(resultLines, "[all]")
				// 修复点：直接使用切片展开语法
				resultLines = append(resultLines, filteredLines...)

				result := strings.Join(resultLines, "\n")
				output.SetText(result)
			} else {
				// 用户点击了“取消”按钮，无需额外处理
			}
		}, w)

		form.Resize(fyne.NewSize(150, 150)) // 调整对话框大小为合理值

		// 显示对话框
		form.Show()
	})

	dedupBtn := widget.NewButton("列去重", func() {
		inputStr := input.Text
		lines := strings.Split(inputStr, "\n") // 按行分割输入文本
		var filteredLines []string

		for _, line := range lines {
			trimmed := strings.TrimSpace(line)
			if trimmed != "" {
				filteredLines = append(filteredLines, trimmed)
			}
		}

		if len(filteredLines) == 0 {
			output.SetText("没有有效的输入")
			return
		}

		// 调用去重函数
		result := deduplicateColumns(filteredLines)
		output.SetText(result)
	})

	clearBtn := widget.NewButton("清除结果", func() {
		input.SetText("")            // 清空输入框
		output.SetText("转换结果会显示在这里") // 恢复默认提示
	})

	copyBtn := widget.NewButton("复制结果", func() {
		clipboard := w.Clipboard() // 使用具体窗口实例的剪贴板接口
		if output.Text != "" {
			clipboard.SetContent(output.Text) // 将输出内容写入剪贴板
			output.SetText("内容已复制到剪贴板")
		} else {
			output.SetText("没有可复制的内容")
		}
	})

	// 创建两个 HBox，每行两个按钮
	firstRow := container.NewHBox(convertBtn, convertSQLBtn)
	secondRow := container.NewHBox(CommaConvertSQLBtn, commaToLineBtn)
	thirdRow := container.NewHBox(dedupBtn, appendCredBtn)
	fourthRow := container.NewHBox(clearBtn, copyBtn)
	content := container.NewVBox(
		widget.NewLabel("输入框："),
		container.NewVBox(input), // 直接添加输入框，无需额外包装
		firstRow,
		secondRow,
		thirdRow,
		fourthRow,
		widget.NewLabel("转换结果："),
		output,
	)

	w.SetContent(content)
	w.ShowAndRun()
}

// 去重函数
func deduplicateColumns(lines []string) string {
	columnMap := make(map[string]struct{})
	var resultLines []string

	for _, line := range lines {
		columns := strings.Split(line, ",")
		var uniqueColumns []string
		for _, col := range columns {
			trimmed := strings.TrimSpace(col)
			if trimmed != "" {
				if _, exists := columnMap[trimmed]; !exists {
					columnMap[trimmed] = struct{}{}
					uniqueColumns = append(uniqueColumns, trimmed)
				}
			}
		}
		resultLines = append(resultLines, strings.Join(uniqueColumns, ","))
	}

	// 将 resultLines 转换为单个字符串
	result := strings.Join(resultLines, "\n")

	// 如果需要进一步处理，可以在这里进行
	line_s := strings.Split(result, "\n") // 按行分割输入文本
	var filteredLines []string

	for _, line_a := range line_s {
		trimmed := strings.TrimSpace(line_a)
		if trimmed != "" {
			filteredLines = append(filteredLines, trimmed)
		}
	}

	// 返回最终结果
	return strings.Join(filteredLines, ",")
}
